# -*- coding: utf-8 -*-
import re
from django import forms

from operation.models import UserAsk


class UserAskForm(forms.ModelForm):

    class Meta:
        model = UserAsk
        fields = ['name', 'mobile', 'course_name']  # 需要验证哪些字段

    def clean_mobile(self):     # clean_字段 为函数名
        """
        验证手机号码是否合法13，14，15，17，18开头
        :return:
        """
        mobile = self.cleaned_data['mobile']
        REGEX_MOBILE = "^1[34578]\d{9}$"
        p = re.compile(REGEX_MOBILE)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError("手机号码非法", code="mobile_invalid")
